package com.fix.hellojava;

import java.util.ArrayList;
import java.util.List;

//共享资源  会场
class Room {
	
	List users= new ArrayList();
	int leftNum;
	int rightNum;
	
	Room(){
		// 100 个座位
		for (int i = 0; i < 100; i++) {
			users.add(new Object());
		}
	}
	
	synchronized boolean walkIn(int p) {
		if (users.size()==0) {  //没有座位了
			return false;
		}
		users.remove(0);   //占用一个座位
		if (p==0) {
			leftNum++;
		}else {
			rightNum++;
		}
		System.out.println("走了哪边"+ p);
		return true;
	}
	
	void info() {
		System.out.println("走前门的用户"+leftNum);
		System.out.println("后门的用户"+rightNum);
	}
	
	
}

//员工
class Emp extends Thread{
	Room room;
	int p;
	
	public Emp(Room room, int p) {
		this.room = room;
		this.p = p;
		this.start();
	}
	
	@Override
	public void run() {
		while(true) {
			int rnd =(int)(Math.random()*10)+1;
			try {
				Thread.sleep(1000/rnd);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			boolean ok =room.walkIn(p);
			if (ok==false) {
				break;
			}
		}
	}
	
}

public class Meeting {
	
	public static void main(String[] args) {
		Room room =new Room();
		Emp emp1=new Emp(room, 0);
		Emp emp2=new Emp(room, 1);
		try {
			emp1.join();
			emp2.join();  //主线程等待自线程执行完毕
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		room.info();
	}

}
